// pages/mt_fillPit_room/mt_fillPit_room.js
var app = getApp()
Page({

  data: {
    pwdArea:false,
    mainArea:true,

    roomInfo:'',            //房间信息
    currentUserCount: 0,    //已入坑人数
    currentTotal: 0,         //已入坑分数
    joinBtnStat:false,       //入坑按钮状态
    room_id:'',              //当前房间id
    isPwd:false              //密码登陆是否正确，后续不再验证密码
  },


  //刷新数据
  _refresh(){
    this._init()
  },


  //输入密码
  _getInputPwd(e) {
    var t = this
    var inputPwd = e.detail.value.inputPwd;
    if (!inputPwd) {
      return;
    }
    var roomInfo = t.data.roomInfo
    if (roomInfo.roomPwd == inputPwd){
      this.setData({
        pwdArea: true,
        mainArea: false
      })
      //记录已验证登陆密码的房间id
      var loginRoomList = app.globalData.fillPit_loginRoomList
      if (loginRoomList.indexOf(roomInfo._id) < 0){
        loginRoomList.push(roomInfo._id)
        app.globalData.fillPit_loginRoomList = loginRoomList
      }
    }else{
      wx.showToast({
        title: '密码不正确！',
        icon: 'loading',
      })
    }
  },

  //返回
  _hidePwdArea() {
    wx.navigateBack()
  },

  onShow: function (){
    //判断是否已验证过密码
    var room_id = this.data.room_id
    if(app.globalData.fillPit_loginRoomList.indexOf(room_id) >=0){
      this.setData({
        pwdArea: true,
        mainArea: false
      })
    }
  },

  onLoad: function (o) {
    var t = this
    if(o._id){
      var rid = o._id

      //清除本地缓存集合及过期
      wx.setStorageSync("roomList", '')
      wx.setStorageSync("roomList_expire", '')
    }else{
      if (app.globalData.fillPit_currentRoom){
        var rid = app.globalData.fillPit_currentRoom._id
      }else{
        return
        var rid = "75777da85ed78074003227be35e5bd9d"
      }
    }


    t.setData({
      room_id:rid
    })
    t._init()
  },

  _init() {
    var t = this
    var room_id = t.data.room_id

    //获取完整房间信息
    t._getRoomInfoForId(room_id, function () {
      var roomInfo = t.data.roomInfo

      //计算入坑人数
      var currentUserCount = 0
      if (roomInfo.userList) {
        var currentUserCount = roomInfo.userList.length
      }

      //计算入坑分数
      var currentTotal = 0
      if (roomInfo.userList) {
        for (var i = 0; i < roomInfo.userList.length; i++) {
          currentTotal += parseInt(roomInfo.userList[i].quota)
        }
      }

      var joinBtnStat = false;


      //判断分数数组是否为空，空了隐藏入坑按钮
      if (roomInfo.quotas.length < 1) {
        joinBtnStat = true
      }

      t.setData({
        currentUserCount: currentUserCount, //入坑人数
        currentTotal: currentTotal,          //入坑分数
        joinBtnStat: joinBtnStat
      })
    })
  },


  onShareAppMessage: function () {
    return {
      title: "一起填大坑！",
      path: "/pages/mt_fillPit_room/mt_fillPit_room?_id=" + app.globalData.fillPit_currentRoom._id
    }
  },







  //入坑
  _joinPit(e){
    wx.showLoading({
      title: '处理中.....',
    })

    // app.globalData.header = e.detail.userInfo.avatarUrl
    // app.globalData.nickName = e.detail.userInfo.nickName

    var t = this
    var roomInfo = this.data.roomInfo
    if(roomInfo.userList){
      for(var i=0;i<roomInfo.userList.length;i++){
        if (roomInfo.userList[i].header == e.detail.userInfo.avatarUrl && roomInfo.userList[i].nickName == e.detail.userInfo.nickName){
          wx.showToast({ title: '用户已入坑！', icon: 'loading' });return
        }
      }
    }
    var update_data = {
      header: e.detail.userInfo.avatarUrl,
      nickName: e.detail.userInfo.nickName,
      _id:roomInfo._id
    }
    app._callYunFun(update_data, 'fillPit_joinPit', function (res) {
      wx.hideLoading()
      switch (res.result) {
        case 'err1': wx.showToast({ title: '缺少参数！', icon: 'loading' }); break;
        case 'err2': wx.showToast({ title: '坑已满！', icon: 'loading' }); break;
        case 'err3': wx.showToast({ title: '用户已入坑！', icon: 'loading' }); break;
        case 'err4': wx.showToast({ title: '服务繁忙！', icon: 'loading' }); break;
        case 'ok': 
          wx.showToast({ title: '操作成功！' })
          t._init()
          break;
      }
    });

  },

  //获取完整房间信息
  _getRoomInfoForId(_id,fun){
    var t = this
    var db = wx.cloud.database({env:"tpcwre1"})
    var tab = db.collection("microTools_fillPit")
    tab.doc(_id).get({
      success:function(res){
        if(res && res.data){
          t.setData({
            roomInfo: res.data
          })
          if(res.data._id && res.data.roomName && res.data.roomPwd){
            var tmpCurrent = {
              _id: res.data._id,
              roomName: res.data.roomName,
              roomPwd: res.data.roomPwd
            }
            //将当前房间信息保存到全局数据中
            app.globalData.fillPit_currentRoom = tmpCurrent
          }
          fun()
        }else{
          wx.showToast({
            title: '数据异常！',
            icon:'loading'
          })
          setTimeout(function(){
            wx.navigateBack()
          },2000)
        }
      }
    })
  }
})